package com.paradigmcreatives.game.listeners;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.paradigmcreatives.game.DoodleGameActivity;
import com.paradigmcreatives.game.R;
import com.paradigmcreatives.game.runtime.RuntimeData;

public class KeyWordKeyListener implements OnKeyListener {

	private static final String TAG = "KeyWordKeyListener";
	DoodleGameActivity activity = null;
	
	
	public KeyWordKeyListener(DoodleGameActivity activity){
		this.activity = activity;
	}
	
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if(KeyEvent.ACTION_UP == event.getAction() && keyCode == 66){
			Log.i(TAG, "Pressed Enter");
			InputMethodManager imm = (InputMethodManager)activity.getSystemService(activity.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			if(v.getTag().toString().equals(activity.getResources().getString(R.string.keyinputtag))){
				RuntimeData.keywordEntered = ((EditText)v).getText().toString();
				Log.i(TAG, "Entered Keyword is " + RuntimeData.keywordEntered);
				activity.uiManager.WordEntered(RuntimeData.keywordEntered);
			}else if(v.getTag().toString().equals(activity.getResources().getString(R.string.keygesstag))){
				String guessedWord = ((EditText)v).getText().toString();
				Log.i(TAG, "gussed word is " + guessedWord);
				boolean guess = false;
				if(guessedWord.equalsIgnoreCase(RuntimeData.keywordEntered)){
					guess = true;
				}
				
				activity.uiManager.guessedKeyword(guess);
			}
		}
		return false;
	}

}
